import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import Audit from '@/store/entities/administration/audit-log/audit';
import Ajax from '@/lib/ajax';
import PageResult from '@/store/entities/page-result';

interface AuditState extends ListState<Audit> {
    excelFile: any;
}

class AuditModule extends ListModule<AuditState, any, Audit> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<Audit>(),
        loading: false,
        excelFile: {},
    };
    actions = {
        async obtainAuditLogs(context: ActionContext<AuditState, any>, payload: any) {
            context.state.loading = true;
            const response = await Ajax.post('/api/services/app/AuditLog/ObtainAuditLogs', payload.data);
            context.state.loading = false;
            const page = response.data.result as PageResult<Audit>;
            context.state.totalCount = page.totalCount;
            context.state.list = page.items;
        },
        async obtainAuditLogsToExcel(context: ActionContext<AuditState, any>, payload: any) {
            const response = await Ajax.post('/api/services/app/AuditLog/ObtainAuditLogsToExcel', payload.data);
            context.state.excelFile = response.data.result;
        },
    };
    mutations = {
        setCurrentPage(state: AuditState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: AuditState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}

const auditModule = new AuditModule();
export default auditModule;
